package com.cyt.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cyt.dao.impl.LunBoTuDaoImpl;
import com.cyt.dao.impl.SPFeiLeiDaoImpl;
import com.cyt.dao.impl.SheJiShiDaoImpl;
import com.cyt.entity.LunBoTu;
import com.cyt.entity.SheJiShi;
import com.cyt.util.BaseServlet;
import com.cyt.util.Result;
import com.google.gson.Gson;


public class AdminLunBoTuServlet extends BaseServlet {
	/**
	 * 查设计师所有
	 * localhost:8080/shiping/adminLunBoTu?reqName=getLunBoTu
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getLunBoTu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     LunBoTuDaoImpl lunDaoi=new LunBoTuDaoImpl();//创建产品分类的实现类对象
	     List <LunBoTu> lunLists= lunDaoi.getLunBoTu();//查到的所有产品分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(lunLists);//将lunLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}

	/**
	 * 查条数
	 * localhost:8080/shiping/adminLunBoTu?reqName=getLunBoTuTiao
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getLunBoTuTiao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     LunBoTuDaoImpl lunDaoi=new LunBoTuDaoImpl();//创建产品分类的实现类对象
	     List <LunBoTu> lunLists= lunDaoi.getLunBoTu();//查到的所有产品分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(lunLists.size());//将lunLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}

	
	/**
	 * localhost:8080/shiping/adminLunBoTu?reqName=getLunBoTuList
	 * 分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getLunBoTuList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     
		response.setCharacterEncoding("UTF-8");
	     PrintWriter out=response.getWriter();
	
		LunBoTuDaoImpl lunDaoi=new LunBoTuDaoImpl();
	     int page=Integer.parseInt(request.getParameter("page"));
		 int size=Integer.parseInt(request.getParameter("size"));
	     List <LunBoTu> lunLists= lunDaoi.getLunBoTuList(page, size);
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     
	     Gson gson=new Gson();
	     String res =gson.toJson(lunLists);//将lunLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	
	/**
	 * 删出
	 * localhost:8080/shiping/adminLunBoTu?reqName=shanChuLunBoTu
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuLunBoTu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    int lunId=Integer.parseInt(request.getParameter("categoryId"));//接收前台传来的参数
	    LunBoTuDaoImpl lunDaoi=new LunBoTuDaoImpl();//创建产品分类的实现类对象
	    int n =lunDaoi.shanChuLunBoTuFL(lunId);//调用删除的方法,获得数据库受影响行数n
	    response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}

	
	/**
	 * localhost:8080/shiping/adminLunBoTu?reqName=shanChuLunBoTuS
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuLunBoTuS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
	    
		LunBoTuDaoImpl lunDaoi=new LunBoTuDaoImpl();

	    
		String ids=request.getParameter("ids");
	    int n =lunDaoi.shanLunBoTuFLS(ids);
	    
	    if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	
	/**
	 * 添加轮播图
	 *  localhost:8080/shiping/adminLunBoTu?reqName=AddLunBoTu
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void AddLunBoTu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    LunBoTuDaoImpl lunDaoi=new LunBoTuDaoImpl();
		
		String lunBoImg=request.getParameter("lunBoImg");
		LunBoTu lbt= new LunBoTu(0, lunBoImg);
        System.out.println(lbt);

		int n= lunDaoi.addLunBoTu(lbt);
		
	    if (n>0) {
			out.print(Result.toClient(true, "添加成功", ""));
		} else {
			out.print(Result.toClient(false, "添加失败", ""));

		}
	    out.flush();
	    out.close();
	}

	
	
	/**
	 * 通过id查数据,前台用于数据回显
	 * localhost:8080/shiping/adminLunBoTu?reqName=chaLunBoTuId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void chaLunBoTuId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
	    LunBoTuDaoImpl lunDaoi=new LunBoTuDaoImpl();
		int lunId=Integer.parseInt(request.getParameter("lunBoId"));
		LunBoTu lunbo= lunDaoi.chaLunBoTuId(lunId);
		Gson gson=new Gson();
		String res =gson.toJson(lunbo);
	    out.print(res);
	    
	    out.flush();
	    out.close();
	}
	
	
	
	/**
	 * 修改
	 * localhost:8080/shiping/adminLunBoTu?reqName=xiuGaiLunBoTu
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void xiuGaiLunBoTu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    LunBoTuDaoImpl lunDaoi=new LunBoTuDaoImpl();
	    
	    
		int lunBoId=0;
		if(request.getParameter("lunBoId")!=null && !request.getParameter("lunBoId").equals("")) {
			lunBoId=Integer.parseInt(request.getParameter("lunBoId"));
		}
		String lunBoImg=request.getParameter("lunBoImg");
		
		LunBoTu lun= new LunBoTu(lunBoId, lunBoImg);
		int n =lunDaoi.xiuGaiLunBoTu(lun);
	    
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "修改成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "修改失败", ""));

		}
	    out.flush();
	    out.close();
	}
	
}
